import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { SystemDeptStateType } from './typings'
import {
  getSystemDeptList,
  getSystemDeptTree,
  postSystemDeptSubmit,
  postSystemDeptRemove,
  getSystemDeptPostList,
  getSelectByTenant,
  postDeptPostSubmit,
  postDeptPostRemove
} from '~/api/system/systemDept'
import { filterValueEnum } from '~/utils/util'

const systemDept: Module<SystemDeptStateType, SystemDeptStateType> = {
  namespaced: true,
  state: {
    parentDeptSelectList: [], // 上级部门 下拉选项
    parentDeptEnum: {} // 上级部门 枚举
  },
  getters: {
    parentDeptSelectList: (state) => state.parentDeptSelectList,
    parentDeptEnum: (state) => state.parentDeptEnum
  },
  mutations: {
    SET_PARENTDEPTSELECTLIST: (state, payload) => {
      state.parentDeptSelectList = payload // 上级部门 下拉
      state.parentDeptEnum = filterValueEnum(payload, 'key', 'title') // id转汉字
    }
  },
  actions: {
    // 获取 部门管理 主列表
    GetSystemDeptList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getSystemDeptList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 获取 上级部门
    GetSystemDeptTree<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getSystemDeptTree<T>(params)
          .then((res: AxiosResponse) => {
            commit('SET_PARENTDEPTSELECTLIST', res?.data?.data || [])
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 部门管理 新增、编辑
    PostSystemDeptSubmit<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postSystemDeptSubmit<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 删除部门
    PostSystemDeptRemove<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postSystemDeptRemove<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 部门岗位 主列表
    GetSystemDeptPostList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getSystemDeptPostList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 部门岗位 岗位
    GetSelectByTenant<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getSelectByTenant<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 部门管理 新增
    PostDeptPostSubmit<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postDeptPostSubmit<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 删除部门
    PostDeptPostRemove<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postDeptPostRemove<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    }
  }
}

export default systemDept
